/*
This software is distributed under the Simplified BSD License:

Copyright (c) 2008, Chris Venter <chris.venter@gmail.com>
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, 
are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice, 
    	this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright notice, 
    	this list of conditions and the following disclaimer in the documentation 
    	and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 
DAMAGE.
*/
#ifndef GENERIK_OPENGL_WINDOW_H
#define GENERIK_OPENGL_WINDOW_H

/** \file window.h
An implementation of an opengl window/viewport.
*/

#include "utils.h"
#include "camera.h"
#include "../tracing.h"

namespace generik
{
	namespace opengl
	{
	
		struct window
		{
			double x, y, width, height;
	
			window()
			: x(0.0), y(0.0)
			, width(1.0), height(1.0)
			{}
	
			virtual void draw() = 0;
			virtual void resize(int w, int h) = 0;
			
			virtual void normalise(int x, int y, double& xout, double& yout)
			{
				xout = x/(double)width - 0.5;
				yout = x/(double)width - 0.5;
			}
		};
		
		struct camerawindow
		: window
		{
			camera* _camera;
			int winw, winh;
			double aspect;
			
			camerawindow(camera* camera_in = 0)
			: _camera(camera_in)
			{}
			
			/*
			void draw()
			{
				GENERIK_TRACER;
				if (_camera)
				{
					OPENGLEXEC(glViewport(x*winw, y*winh, width*winw, height*winh));
					_camera->draw();
				}
				else
				{
					GENERIK_TRACE("window has no attached camera");
				}
			}
			*/
			void draw()
			{
				OPENGLEXEC(glViewport(x*winw, y*winh, width*winw, height*winh));
			}
			
			void setsize(int w, int h)
			{
				winw = w;
				winh = h;
				aspect = (double)w/(double)h;
				if (_camera) _camera->setaspect(aspect);
			}
			
			void resize()
			{
				if (_camera) _camera->loadfrustum();
			}
			
			void resize(int w, int h)
			{
				/*
				GENERIK_TRACER;
				GENERIK_TRACE("resizeing to ("<<w<<", "<<h<<")");
				winw = w;
				winh = h;
				aspect = (double)w/(double)h;
				if (_camera)
				{
					GENERIK_TRACE("setting up camera");
					_camera->setaspect(aspect);
					_camera->loadfrustum();
				}
				*/
				setsize(w, h);
				resize();
			}
			
			void normalise(int x, int y, double& xout, double& yout)
			{
				if (aspect > 1.0)
				{
					xout = +2.0*(double)x/(double)winh - aspect;
					yout = -2.0*(double)y/(double)winh + 1.0;
				}
				else
				{
					xout = +2.0*(double)x/(double)winw - 1.0;
					yout = -2.0*(double)y/(double)winw + 1.0/aspect;
				}
			}
		};
		
		/*
		struct parentwindow
		: window
		{
			std::list<window*> windows;
			
			void draw()
			{
				for (std::list<window*>::iterator i = windows.begin(); i != windows.end(); ++i)
				{
					(*i)->draw();
				}
			}
			void resize(int w, int h)
			{
				int xi = (x*w);
				int yi = (y*h);
				int wi = (width*w);
				int hi = (height*h);
			}
		};
		*/
		
	}
}

#endif









